package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.annotation.BackLoginToken;
import com.sidianzhong.sdz.annotation.UserLoginToken;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.service.commond.RedisService;
import com.sidianzhong.sdz.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "充值")
@Controller
public class RechargeController {

    @Autowired
    RechargeService rechargeService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @UserLoginToken
    @ApiOperation(value = "创建'充值'表中一条信息")
    @RequestMapping(value = "/recharge/new", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createRecharge(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "rechargeDate", required = false) String rechargeDate,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "blockNum", required = false) Integer blockNum,
            @RequestParam(value = "fromAddress", required = false) String fromAddress,
            @RequestParam(value = "toAddress", required = false) String toAddress,
            @RequestParam(value = "txRemarks", required = false) String txRemarks
    ) {
        Recharge item = new Recharge();
        Date date = new Date();
        if (userId != null) {
            item.setUserId(userId);
        }
        if (coinId != null) {
            item.setCoinId(coinId);
        }
        if (rechargeDate != null) {
            item.setRechargeDate(date);
        }
        if (amount != null) {
            item.setAmount(amount);
        }
        if (blockNum != null) {
            item.setBlockNum(blockNum);
        }
        if (fromAddress != null) {
            item.setFromAddress(fromAddress);
        }
        if (toAddress != null) {
            item.setToAddress(toAddress);
        }
        if (txRemarks != null) {
            item.setTxRemarks(txRemarks);
        }
        Recharge iteams = rechargeService.create(item);
        boolean result = iteams != null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "删除'充值'表中的某条记录")
    @RequestMapping(value = "/recharge/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteRecharge(
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
        Recharge item = rechargeService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        int delete = rechargeService.delete(id);
        boolean result = delete != 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "修改'充值'表中的某条记录")
    @RequestMapping(value = "/recharge/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editRecharge(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "rechargeDate", required = false) String rechargeDate,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "blockNum", required = false) Integer blockNum,
            @RequestParam(value = "fromAddress", required = false) String fromAddress,
            @RequestParam(value = "toAddress", required = false) String toAddress,
            @RequestParam(value = "txRemarks", required = false) String txRemarks
    ) {
        Recharge item = rechargeService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        if (userId != null) {
            item.setUserId(userId);
        }
        if (coinId != null) {
            item.setCoinId(coinId);
        }
        if (rechargeDate != null) {
            item.setRechargeDate(Tools.convertStringToDate(rechargeDate, "yyyy-MM-dd HH:mm:ss"));
        }
        if (amount != null) {
            item.setAmount(amount);
        }
        if (blockNum != null) {
            item.setBlockNum(blockNum);
        }
        if (fromAddress != null) {
            item.setFromAddress(fromAddress);
        }
        if (toAddress != null) {
            item.setToAddress(toAddress);
        }
        if (txRemarks != null) {
            item.setTxRemarks(txRemarks);
        }
        int update = rechargeService.update(item);
        boolean result = update != 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "查询'充值'表中的某条记录")
    @RequestMapping(value = "/getRechargeById", method = {RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getRechargeById(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
        Recharge item = rechargeService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "查询'充值'表中的多条记录返回对象")
    @RequestMapping(value = "/recharges", method = {RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getRecharges(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "rechargeDate", required = false) String rechargeDate,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "blockNum", required = false) Integer blockNum,
            @RequestParam(value = "fromAddress", required = false) String fromAddress,
            @RequestParam(value = "toAddress", required = false) String toAddress,
            @RequestParam(value = "txRemarks", required = false) String txRemarks
    ) {
        Recharge item = new Recharge();
        if (userId != null) {
            item.setUserId(userId);
        }
        if (coinId != null) {
            item.setCoinId(coinId);
        }
        if (rechargeDate != null) {
            item.setRechargeDate(Tools.convertStringToDate(rechargeDate, "yyyy-MM-dd HH:mm:ss"));
        }
        if (amount != null) {
            item.setAmount(amount);
        }
        if (blockNum != null) {
            item.setBlockNum(blockNum);
        }
        if (fromAddress != null) {
            item.setFromAddress(fromAddress);
        }
        if (toAddress != null) {
            item.setToAddress(toAddress);
        }
        if (txRemarks != null) {
            item.setTxRemarks(txRemarks);
        }
        PageInfo<Recharge> list = rechargeService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder, item);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "查询'充值'表中的多条记录返回自定义JSONObject")
    @RequestMapping(value = "/rechargesObject", method = {RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getRechargesObject(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "rechargeDate", required = false) String rechargeDate,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "blockNum", required = false) Integer blockNum,
            @RequestParam(value = "fromAddress", required = false) String fromAddress,
            @RequestParam(value = "toAddress", required = false) String toAddress,
            @RequestParam(value = "txRemarks", required = false) String txRemarks
    ) {
        Recharge item = new Recharge();
        if (userId != null) {
            item.setUserId(userId);
        }
        if (coinId != null) {
            item.setCoinId(coinId);
        }
        if (rechargeDate != null) {
            item.setRechargeDate(Tools.convertStringToDate(rechargeDate, "yyyy-MM-dd HH:mm:ss"));
        }
        if (amount != null) {
            item.setAmount(amount);
        }
        if (blockNum != null) {
            item.setBlockNum(blockNum);
        }
        if (fromAddress != null) {
            item.setFromAddress(fromAddress);
        }
        if (toAddress != null) {
            item.setToAddress(toAddress);
        }
        if (txRemarks != null) {
            item.setTxRemarks(txRemarks);
        }
        PageInfo<JSONObject> list = rechargeService.getListWithObject(pageNum, pageSize, sortItem, sortOrder, item);
        List<JSONObject> list1 = list.getList();
        List<JSONObject> collect = list1.stream().map(ls -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", ls.getInteger("id"));
            jsonObject.put("userId", ls.getInteger("userId"));
            jsonObject.put("coinId", ls.getInteger("coinId"));
            jsonObject.put("rechargeDate", ls.getDate("rechargeDate"));
            jsonObject.put("amount", ls.getDouble("amount"));
            jsonObject.put("blockNum", ls.getInteger("blockNum"));
            jsonObject.put("fromAddress", ls.getString("fromAddress"));
            jsonObject.put("toAddress", ls.getString("toAddress"));
            jsonObject.put("txRemarks", ls.getString("txRemarks"));
            return jsonObject;
        }).collect(Collectors.toList());
        list.setList(collect);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }
}
